<template>
    <div>
        <NavButtons />

        <div class="play-page fullscreen">
            <h2 class="title">🎮 休闲小游戏</h2>

            <div class="game-list">
                <div v-for="game in games" :key="game.id" class="game-card">
                    <img :src="getImagePath(game.img)" alt="游戏图标" class="game-icon" />
                    <h3>{{ game.title }}</h3>
                    <p class="subtitle">{{ game.desc }}</p>
                    <button class="play-btn" @click="playGame(game)">Play Now</button>
                </div>
            </div>

            <!-- 弹窗浮窗游戏界面 -->
            <div v-if="showModal" class="modal-overlay" @click.self="closeGame">
                <div class="modal-window transparent"
                    :style="{ width: modalWidth + 'px', height: modalTotalHeight + 'px' }">
                    <div class="modal-header">
                        <h3>{{ currentGameTitle }}</h3>
                        <button class="modal-close" @click="closeGame">×</button>
                    </div>
                    <iframe :src="currentGameUrl" class="modal-iframe"
                        :style="{ width: modalWidth + 'px', height: modalHeight + 'px' }" allowfullscreen></iframe>
                </div>
            </div>
        </div>
        <!-- 页脚 -->
        <footer class="footer">
            <span>© 2025 复古游戏博物馆 | Retro Game Museum</span>
            <br />
            <span>致谢</span>
            <br />
            <a href="https://github.com/gabrielecirulli/2048" target="_blank" style="color:#b3e5fc;">https://github.com/gabrielecirulli/2048</a>
            <br />
            <a href="https://github.com/mumuy/pacman" target="_blank" style="color:#b3e5fc;">https://github.com/mumuy/pacman</a>
            <br />
            <a href="https://github.com/CoolMrp/AirplaneGames" target="_blank" style="color:#b3e5fc;">https://github.com/CoolMrp/AirplaneGames</a>
        </footer>
    </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import NavButtons from '/src/components/NavButtons.vue'
import gameData from '@/assets/data/gamePlay.json'

const games = ref([])
const currentGameUrl = ref('')
const currentGameTitle = ref('')
const modalWidth = ref(800)
const modalHeight = ref(600)
const modalTotalHeight = ref(640)
const showModal = ref(false)

const imageMap = import.meta.glob('@/assets/img_play/*', {
    eager: true,
    import: 'default'
})

function getImagePath(imgName) {
    return imageMap[`/src/assets/img_play/${imgName}`] || ''
}

function playGame(game) {
    currentGameUrl.value = game.gamePath
    currentGameTitle.value = game.title
    modalWidth.value = game.width || 640
    modalHeight.value = game.height || 640
    modalTotalHeight.value = modalHeight.value + 48 // header 高度加 margin
    showModal.value = true
}

function closeGame() {
    showModal.value = false
    currentGameUrl.value = ''
}

function disableScrollKeys(e) {
    const keys = [32, 37, 38, 39, 40]
    if (keys.includes(e.keyCode)) {
        e.preventDefault()
    }
}

onMounted(() => {
    games.value = gameData
    document.body.classList.add('layout-play')
    window.addEventListener('keydown', disableScrollKeys)
})
onUnmounted(() => {
    document.body.classList.remove('layout-play')
    window.removeEventListener('keydown', disableScrollKeys)
})
</script>

<style scoped>
/* 页脚样式 */
.footer {
    width: 100%;
    text-align: center;
    color: #eee;
    font-size: 15px;
    padding: 18px 0 10px 0;
    background: rgba(20, 20, 50, 0.7);
    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 100;
    letter-spacing: 1px;
    box-shadow: 0 -2px 12px rgba(0,0,0,0.15);
}

.play-page {
    padding: 40px;
    text-align: center;
    min-height: 100vh;
    background: url('@/assets/img_play/bg.jpg') no-repeat center center;
    background-size: cover;
    overflow: hidden;
}

.title {
    font-size: 28px;
    color: white;
    margin-bottom: 20px;
    text-shadow: 0 2px 5px black;
}

.game-list {
    display: flex;
    justify-content: center;
    gap: 24px;
    flex-wrap: wrap;
}

.game-card {
    background: rgba(20, 20, 50, 0.85);
    padding: 16px;
    border-radius: 20px;
    width: 180px;
    color: white;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: transform 0.3s;
}

.game-card:hover {
    transform: translateY(-6px);
}

.game-icon {
    width: 100px;
    height: 100px;
    object-fit: cover;
    border-radius: 16px;
    background-color: white;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
    margin-bottom: 10px;
}

.subtitle {
    font-size: 14px;
    opacity: 0.8;
    margin-bottom: 12px;
}

.play-btn {
    background: #7c4dff;
    border: none;
    padding: 8px 14px;
    color: white;
    border-radius: 12px;
    cursor: pointer;
    font-weight: bold;
}

.play-btn:hover {
    background: #651fff;
}

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
}

.modal-window {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 16px;
    overflow: hidden;
    box-sizing: content-box;
    box-shadow: 0 0 24px rgba(0, 0, 0, 0.4);
    padding: 0;
    margin: 0;
    backdrop-filter: blur(12px);
}

.modal-header {
    background-color: transparent;
    padding: 8px 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: bold;
    height: 40px;
    color: #fff;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    color: #ccc;
    cursor: pointer;
    padding: 0 4px;
}

.modal-close:hover {
    color: #f66;
}

.modal-iframe {
    display: block;
    border: none;
    overflow: hidden;
}
</style>

<style>
body.layout-play {
    overflow: hidden !important;
    position: fixed;
    width: 100%;
    height: 100%;
}

body.layout-play #app {
    max-width: none !important;
    padding: 0 !important;
    margin: 0 !important;
    width: 100% !important;
    height: 100% !important;
}
</style>